package com.wanxi.dao.product.impl;

import com.wanxi.dao.product.IProductDao;
import com.wanxi.domain.product.ProductModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ProductDaoImpl {

    JDBC jdbc = new JDBC();
    
  /*  public List<ProductModel> findAll(String page, String limit) {

        String sql = "select b.*,bt.name typeName from books b inner join books_type bt on b.type_id = bt.id  limit  ?,?";

        List<Object> data = new ArrayList<>();
        data.add((Integer.parseInt(page)-1)*Integer.parseInt(limit));
        data.add(Integer.parseInt(limit));

        ResultSet resultSet =  jdbc.findAll(sql,data);

        List<ProductModel> productModels = new ArrayList<>();


        while (true) {
            try {
                if (!resultSet.next()) break;
                ProductModel productModel = new ProductModel();

                productModel.setId(resultSet.getInt("id"));
                productModel.setTypeName(resultSet.getString("typeName"));
                productModel.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                productModel.setEnable(resultSet.getInt("enable"));
                productModel.setName(resultSet.getString("name"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setDescription(resultSet.getString("content"));
                productModel.setForWho(resultSet.getString("for_who"));

                productModels.add(productModel);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return productModels;
    }*/

    //跟据书本类型查询书本
    /*  public List<ProductModel> findByTypeName(String name, String page, String limit) {

        String sql = "select b.*,bt.name typeName from books b inner join books_type bt on b.type_id = bt.id and bt.name = ? limit " + (Integer.parseInt(page) - 1) * Integer.parseInt(limit) + "," + Integer.parseInt(limit);
        List<Object> data = new ArrayList<>();

        data.add(name);

        ResultSet resultSet = jdbc.findAll(sql, data);

        List<ProductModel> productModels = new ArrayList<>();

        while (true) {
            try {
                if (!resultSet.next()) break;
                ProductModel productModel = new ProductModel();

                productModel.setId(resultSet.getInt("id"));
                productModel.setTypeName(resultSet.getString("typeName"));
                productModel.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                productModel.setEnable(resultSet.getInt("enable"));
                productModel.setName(resultSet.getString("name"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setDescription(resultSet.getString("content"));
                productModel.setForWho(resultSet.getString("for_who"));

                productModels.add(productModel);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return productModels;
    }*/

    //跟据产品类型查询数量
   /* public int getCountType(String typeName) {
        String sql = "select count(*) count from (select b.* from books b inner join books_type bt on b.type_id = bt.id and bt.name = '" + typeName + "' ) a";

        return jdbc.getCount(sql);

    }*/

    //增加产品信息

    /*public int insert(ProductModel productModel) {
        String sql = "insert into books \n" +
                "(name,normal_price,content,img_href,create_time,update_time,enable,type_id,for_who)\n" +
                "values(?,?,?,?,now(),now()," + productModel.getEnable() + "," + productModel.getTypeId() + ",?)\n";

        List<Object> data = new ArrayList<>();

        data.add(productModel.getName());
        data.add(productModel.getNormalPrice());
        data.add(productModel.getDescription());
        data.add(productModel.getImgHref());
        data.add(productModel.getForWho());

        int i = jdbc.insertUser(sql, data);

        return i;
    }*/

    //查询上下个分页产品

    public List<ProductModel> findSwitchInfo(String id) {
        String sql = "select \n" +
                "\tid,\n" +
                "\timg_href ,\n" +
                "\tname ,\n" +
                "\tnormal_price ,-- 当前价格\n" +
                "\tprice , -- 市场价\n" +
                "\tcontent \n" +
                "from books b\n" +
                "where id = (select max(id) from books b2 where id <?)\n" +
                "\n" +
                "union \n" +
                "\n" +
                "select\n" +
                "\tid,\n" +
                "\timg_href ,\n" +
                "\tname ,\n" +
                "\tnormal_price ,-- 当前价格\n" +
                "\tprice , -- 市场价\n" +
                "\tcontent \n" +
                "from books b\n" +
                "where id = ?\n" +
                "\n" +
                "union \n" +
                "\n" +
                "select\n" +
                "\tid,\n" +
                "\timg_href ,\n" +
                "\tname ,\n" +
                "\tnormal_price ,-- 当前价格\n" +
                "\tprice , -- 过期价格\n" +
                "\tcontent \n" +
                "from books b\n" +
                "where id = (select min(id) from books b2 where id >?)\n";

        List<Object> data = new ArrayList<>();
        data.add(id);
        data.add(id);
        data.add(id);

        ResultSet all = jdbc.findAll(sql, data);

        List<ProductModel> list = new ArrayList<>();
        while (true) {
            try {
                if (!all.next()) break;
                ProductModel productModel = new ProductModel();

                productModel.setImgHref(all.getString("img_href"));
                productModel.setName(all.getString("name"));
                productModel.setNormalPrice(all.getBigDecimal("normal_price"));
                productModel.setPrice(all.getBigDecimal("price"));
                productModel.setDescription(all.getString("content"));
                productModel.setId(Integer.parseInt(all.getString("id")));

                list.add(productModel);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;

    }

    //查询产品条数
  /*  public int getCount() {

        String sql = "select count(*) count from (select b.*,bt.name typeName from books b inner join books_type bt on b.type_id = bt.id) a";

        return jdbc.getCount(sql);
    }*/


    //查询前端产品的条数
   /* public int getFrontCount() {
        String sql = "select count(*) count from books ";

        return jdbc.getCount(sql);
    }
*/

    //删除产品信息
    /*public int deleteById(String id) {

        String sql = "delete from books where id=?";
        ArrayList<Object> data = new ArrayList<>();
        data.add(id);

        int num = jdbc.deleteById(sql, data);
        return num;
    }*/

    //通过id查询
   /*
    public ProductModel findById(String id) {

        String sql ="select b.*,bt.name typeName from books b inner join books_type bt on b.type_id = bt.id where b.id=?";
        ArrayList<Object> data = new ArrayList<>();
        data.add(id);

        ResultSet resultSet =jdbc.findByID(sql,data);
        ProductModel productModel = new ProductModel();

        try {
            resultSet.next();
            productModel.setId(resultSet.getInt("id"));
            productModel.setTypeName(resultSet.getString("typeName"));
            productModel.setNormalPrice(resultSet.getBigDecimal("normal_price"));
            productModel.setEnable(resultSet.getInt("enable"));
            productModel.setName(resultSet.getString("name"));
            productModel.setImgHref(resultSet.getString("img_href"));
            productModel.setDescription(resultSet.getString("content"));
            productModel.setForWho(resultSet.getString("for_who"));

            return productModel;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }*/

    //更新产品信息
 /*   public int update(ProductModel productModel) {

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        ArrayList<Object> data = new ArrayList<>();
        String sql = "update books  " +
                "set name = case when 'null' = '" +productModel.getName()+"' then name else ? end,"+
                " normal_price = case when 'null' = '" +productModel.getNormalPrice()+"' then normal_price else ? end," +
                " img_href = case when 'null' = '" +productModel.getImgHref()+"' then img_href else ? end,"+
                "enable = case when 'null' = '" +productModel.getEnable()+"' then enable else ? end,"+
                "content = case when 'null' = '" +productModel.getDescription()+"' then content else ? end,"+
                "type_id = case when 'null' = '" +productModel.getTypeId()+"' then type_id else ? end,"+
                "for_who = case when 'null' = '" +productModel.getForWho()+"' then for_who else ? end,"+
                "update_time = ? " +
                "where id = ? ";

        data.add(productModel.getName());
        data.add(productModel.getNormalPrice());
        data.add(productModel.getImgHref());
        data.add(productModel.getEnable());
        data.add(productModel.getDescription());
        data.add(productModel.getTypeId());
        data.add(productModel.getForWho());
        data.add(simpleDateFormat.format(date));
        data.add(productModel.getId());

        int num = jdbc.update(sql,data);
        return num;
    }*/


}
